package com.ellricky.baselib.ui.course

import android.view.View
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.IdNameEntity
import com.ellricky.baselib.entity.course.FileDetailEntity
import com.ellricky.baselib.entity.new.CourseType
import com.ellricky.baselib.entity.new.WareDetail
import com.ellricky.baselib.http.Api.API_COURSE_FILE_LEARN_DETAIL
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.http.callback.ECallback
import com.ellricky.baselib.ui.base.BaseDetailActivity
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.baselib.util.NewEntityCallback
import kotlinx.android.synthetic.main.base_activity_course_file_learn_detail.*
import java.util.HashMap

class FileLearnDetailActivity : BaseDetailActivity() {

    private var fileUrl: String? = null
    private var wordUrl: String? = null

    override fun getChildContentLayout(): Int {
        return R.layout.base_activity_course_file_learn_detail
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.base_course_detail)

        btnFile.setOnClickListener(this)
        btnWord.setOnClickListener(this)

        loadDetail()
    }

    private fun loadDetail() {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherWareDetail"
        paramMap["ware_id"] = id
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<WareDetail>(this) {
                override fun onSuccess(detail: WareDetail, var2: String?) {
                    tvNo.value = detail.ware_code
                    tvCourseType.value = detail.course_type_name
                    tvPublishTime.value = detail.ware_createtime
                    tvAgeRange.value = detail.ware_stage_name
                    fileUrl = detail.ware_file
                    wordUrl = detail.ware_copy
                }
            })
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            btnFile -> {
                EAppUtil.startFileViewerPageOnDetail(this, ".pdf", fileUrl)
            }
            btnWord -> {
                if (wordUrl?.isEmpty() != false) {
                    ToastUtil.show(this, "暂无文案")
                } else {
                    ToastUtil.show(this, wordUrl ?: "")
                }
            }
        }
    }
}
